#include "monitorform.h"
#include "ui_monitorform.h"
#include "color.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <QDebug>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QTime>
#define REFRESH_TIME 1500

monitorForm::monitorForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::monitorForm),
    timer(new QTimer(this)),
    label(new QLabel(this))
{
    ui->setupUi(this);
}

monitorForm::~monitorForm()
{
    delete ui;
    delete image;
//    timer->stop();
}

void monitorForm::repaintImage()
{
/*    QTime time;

    time.start();


    QPixmap pixmap;
    if(GetImage(&pixmap)<0)
        return;
    qDebug()<<time.elapsed()/1000.0<<"s GetImage.";
    pixmap = pixmap.scaled(this->size());
    this->setAutoFillBackground(true);
    qDebug()<<time.elapsed()/1000.0<<"s Scaled.";
    QPalette palette;
    palette.setBrush(this->backgroundRole(), QBrush(pixmap));
    this->setPalette(palette);
    this->repaint();

    qDebug()<<time.elapsed()/1000.0<<"s Repaint.";
*/
}
void monitorForm::showwin(int win,bool bVisible)
{
    if(bVisible)
    {
        edid_state=(win==win_EDID);
        signalinfo_state=(win==win_Signal_Info);
        error_rate_state=(win==win_Error_Rate);
    }
    else
    {
        edid_state=signalinfo_state=error_rate_state=false;
    }
}

bool monitorForm::eventFilter(QObject *watched,QEvent *event)
{
        if(event->type()==QEvent::KeyPress)
        {
            QKeyEvent *key_event=static_cast<QKeyEvent *>(event);
            if(key_event->key()==Qt::Key_Left)
                focusPreviousChild();
            else if(key_event->key()==Qt::Key_Right)
                focusNextChild();
        }
        return QWidget::eventFilter(watched,event);
}

void monitorForm::keyPressEvent(QKeyEvent *event)
{
    int key=event->key();
    qDebug()<<key;
    switch(key)
        {
        case win_EDID:
            edid_state=!edid_state;
            showwin(win_EDID,edid_state);
            break;
        case win_Signal_Info:
            signalinfo_state=!signalinfo_state;
            showwin(win_Signal_Info,signalinfo_state);
            break;
        case win_Error_Rate:
            error_rate_state=!error_rate_state;
            showwin(win_Error_Rate,error_rate_state);
            break;
        case Qt::Key_F12:
        case Qt::Key_Exit:
            if(edid_state||signalinfo_state||error_rate_state)
                showwin(win_EDID,false);
            else
                if(ShowPreWin())
                    this->hide();
            break;
        default:
            if(ShowWin(key,0))
                this->hide();
            break;
        }

}

void monitorForm::hideEvent(QHideEvent *event)
{
//    timer->stop();
}

void monitorForm::showEvent(QShowEvent *event)
{
}


